package com.tnxst.phexamination.fragment;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProviders;
import com.tnxst.phexamination.R;
import com.tnxst.phexamination.activity.DataSyncActivity;
import com.tnxst.phexamination.activity.GoDoorRecordActivity;
import com.tnxst.phexamination.activity.IndexActivity;
import com.tnxst.phexamination.activity.LoginActivity;
import com.tnxst.phexamination.activity.PackageServiceActivity;
import com.tnxst.phexamination.activity.SignManagementActivity;
import com.tnxst.phexamination.adapter.ImageAdapter;
import com.tnxst.phexamination.databinding.IndexFragmentBinding;
import com.tnxst.phexamination.utils.Utils;
import com.tnxst.phexamination.viewmodel.GlobalViewModel;
import com.youth.banner.indicator.CircleIndicator;

import java.util.ArrayList;
import java.util.List;


public class IndexFragment extends Fragment implements View.OnClickListener {

    private IndexFragmentBinding binding;

    private GlobalViewModel viewModel;

    private Context context;

    private List<Object> path;


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        binding = DataBindingUtil.inflate(inflater, R.layout.index_fragment, container, false);
        return binding.getRoot();
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        viewModel = ViewModelProviders.of(requireActivity()).get(GlobalViewModel.class);
        context = getActivity();
        initBannerView();
        useBanner();
        initView();
    }


    private void initView() {
        binding.dataSync.setOnClickListener(this);
        binding.servicePake.setOnClickListener(this);
        binding.signManage.setOnClickListener(this);
        binding.goDoorRecord.setOnClickListener(this);
    }


    @Nullable
    @Override
    public View getView() {
        return binding.getRoot();
    }

    private void initBannerView() {
        path = new ArrayList<>();

        path.add(R.drawable.home_banner_one);
        path.add(R.drawable.home_banner_two);
        path.add(R.drawable.home_banner_three);
        path.add(R.drawable.home_banner_four);
    }

    private void useBanner() {
        //--------------------------简单使用-------------------------------
        binding.banner.addBannerLifecycleObserver(this)//添加生命周期观察者
                .setAdapter(new ImageAdapter(path, context))
                .setIndicator(new CircleIndicator(context))
                .start();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        binding.banner.destroy();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.service_pake://服务包管理
                Intent intent = new Intent(context, PackageServiceActivity.class);
                startActivity(intent);
                break;
            case R.id.go_door_record: //上门记录
                Intent goDoorRecord = new Intent(context, GoDoorRecordActivity.class);
                startActivity(goDoorRecord);
                break;
            case R.id.sign_manage://签约管理
                Intent sign = new Intent(context, SignManagementActivity.class);
                startActivity(sign);
                break;
            case R.id.data_sync://数据同步
                Intent dataSync = new Intent(context, DataSyncActivity.class);
                startActivity(dataSync);
                break;
        }

    }
}
